
package com.probiz.estore.customer.dao.impl;

import java.util.Date;

import com.probiz.estore.common.model.customer.ValidationSession;
import com.probiz.estore.core.dao.impl.HibernateGenericDaoImpl;
import com.probiz.estore.customer.dao.ValidationSessionDao;

/**
 * ValidationSession Data Access Object (DAO) implementation. Developer
 * introduced interfaces should be declared here. Won't get overwritten.
 */
public class ValidationSessionDaoImpl extends HibernateGenericDaoImpl<ValidationSession> 
implements ValidationSessionDao {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.customer.dao.ValidationSessionDao#isUrlValid(java.lang.String,
	 *      java.lang.String, java.lang.Short)
	 */
	public boolean isUrlValid(String email, String url, Short type) {

		String hql = "select count(*) from ValidationSession vo where vo.expiredDate>? and vo.email=? and vo.url=? and vo.vsType=?";

		Date date = new Date();

		Object[] params = new Object[] { date, email, url, type };

		Long value = (Long) findUnique(hql, params);

		logger.debug("isUrlValid:value=====>" + value);

		if (value.intValue() > 0)
			return true;
		else
			return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.customer.dao.ValidationSessionDao#isUrlValid(java.lang.String,
	 *      java.lang.Short)
	 */
	public boolean isUrlValid(String url, Short type) {

		String hql = "select count(*) from ValidationSession vo where vo.expiredDate>? and vo.url=? and vo.vsType=?";
		Date date = new Date();
		Object[] params = new Object[] { date, url, type };

		Long value = (Long) findUnique(hql, params);
		logger.debug("isUrlValid: value=====>" + value);
		if (value.intValue() > 0)
			return true;
		else
			return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.customer.dao.ValidationSessionDao#getByUrlType(java.lang.String,
	 *      java.lang.Short)
	 */
	public ValidationSession getByUrlType(String url, Short type) {
		String hql = "from ValidationSession vo where vo.url=? and vo.vsType=? and vo.expiredDate>?";
		Object obj = this.findUnique(hql, url, type.shortValue(), new Date());
		return (ValidationSession) obj;
	}
}